document.addEventListener('DOMContentLoaded', function() {
    // 加载所有职位数据
    function loadAllJobs() {
        const token = localStorage.getItem('token');
        if (!token) {
            window.location.href = 'admin-login.html';
            return;
        }

        fetch('http://localhost:3001/api/jobs/admin/all-jobs', {
            method: 'GET',
            headers: {
                'Authorization': `Bearer ${token}`,
                'Content-Type': 'application/json'
            }
        })
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应不正常');
            }
            return response.json();
        })
        .then(data => {
            if (data.success) {
                renderJobList(data.data);
            } else {
                showError(data.message || '获取职位列表失败');
            }
        })
        .catch(error => {
            console.error('获取职位列表时出错:', error);
            showError('获取职位列表失败，请稍后重试');
        });
    }

    // 渲染职位列表
    function renderJobList(jobs) {
        const container = document.getElementById('admin-job-list');
        if (!container) return;

        if (jobs.length === 0) {
            container.innerHTML = '<div class="no-data">暂无职位数据</div>';
            return;
        }

        let html = `
            <table class="job-table">
                <thead>
                    <tr>
                        <th>职位名称</th>
                        <th>公司</th>
                        <th>薪资范围</th>
                        <th>工作地点</th>
                        <th>状态</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
        `;

        jobs.forEach(job => {
            const statusClass = job.status === 'active' ? 'status-active' : 'status-inactive';
            const statusText = job.status === 'active' ? '已上架' : '已下架';
            const publishDate = new Date(job.createdAt).toLocaleDateString();

            html += `
                <tr>
                    <td>${escapeHtml(job.title)}</td>
                    <td>${escapeHtml(job.industry||'-')}</td>
                    <td>${escapeHtml(job.education||'-')}</td>
                    <td>${escapeHtml(job.experience||'-')}</td>
                    <td>${escapeHtml(job.salaryRange||'-')}</td>
                    <td>${escapeHtml(job.location||'-')}</td>
                    <td>${escapeHtml(job.address||'-')}</td>
                    <td><span class="status ${statusClass}">${statusText}</span></td>
                    <td>${publishDate}</td>
                    <td>
                        <button class="btn edit-btn" onclick="editJob('${job.id}')">编辑</button>
                        <button class="btn ${job.status === 'active' ? 'down-btn' : 'up-btn'}" onclick="toggleJobStatus('${job.id}', '${job.status === 'active' ? 'inactive' : 'active'}')">${job.status === 'active' ? '下架' : '上架'}</button>
                        <button class="btn delete-btn" onclick="deleteJob('${job.id}')">删除</button>"}]}
                    </td>
                </tr>
            `;
        });

        html += `
                </tbody>
            </table>
        `;
        container.innerHTML = html;
    }

    // 辅助函数：转义HTML特殊字符
    function escapeHtml(text) {
        return text
            .replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/